package controller.user;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import pojo.User;
import service.user.UserService;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    //controller层调service层
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;
    @RequestMapping(value ="/login",method = RequestMethod.GET)
    public String Login(Model model,HttpSession session){
        if (session.getAttribute("cid")!=null){
            session.removeAttribute("cid");
        }
        return "../login";
    }
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String isLogin(Model model, @Param("cid") int cid, @Param("password") String password, HttpSession session){
        User user = userService.queryUserById(cid);
        if (user!=null){
            if (user.getPassword().equals(password)){
                if (session.getAttribute("cid")!=null){
                    session.removeAttribute("cid");
                }
                session.setAttribute("cid",user);
                session.setMaxInactiveInterval(3600);//设置会话最大维持时间1小时
                return "main";
            }
            else{
                model.addAttribute("error","密码错误");
                return "../login";//返回登录界面
            }
        }else {
            model.addAttribute("error","员工id错误");
            return "../login";//返回登录界面
        }
    }
}
